package edu.towson.cosc.cosc455.ryanhoran;

import java.util.Scanner;
import java.io.*;

public class InputOutput {
			
	//read the input document
	public void read() throws IOException, CompilerException{
		Scanner in = new Scanner(System.in);
		String fileName;
		System.out.print("Please enter your file path:");
		fileName = in.nextLine();
		if(!fileName.endsWith(".mkd")){
			error("Incorrect file format. ");
		}
		BufferedReader read = new BufferedReader(new FileReader(fileName));
		try {
			StringBuilder build = new StringBuilder();
			String line = read.readLine();
			while (line != null){
				build.append(line);
				build.append(" ");
				line = read.readLine();
				globalVar.mkd = build.toString();
			}
		}
		catch (IOException e){
			e.printStackTrace();
		}
		 finally {
			read.close();
		}
		if (globalVar.mkd != null){
		new SynAnalyzer().document();
		} else{
			error("File not read. ");
		}
	}
	//output final document
	public void write(){
		BufferedWriter Writer = null;
		try{
			Writer = new BufferedWriter( new FileWriter("Final.html"));
			while(!(globalVar.html.empty())){
				Writer.write(globalVar.html.pop());
			}
		}
		catch (IOException e){
			
		}
		finally {
			try{
				if (Writer != null){
					Writer.close();
				}
			}
			catch (IOException e){
				
			}
		}
	}
	
	//Exception Handling
	public void error(String message){
		System.out.printf("Error in Input. %n%s%n Program Closing.", message);
		System.exit(0);
	}
	
}
